{***************************************************************************}
{                                                                           }
{           Spring Framework for Delphi                                     }
{                                                                           }
{           Copyright (C) 2009-2011 DevJET                                  }
{                                                                           }
{           http://www.spring4d.org                                         }
{                                                                           }
{***************************************************************************}
{                                                                           }
{  Licensed under the Apache License, Version 2.0 (the "License");          }
{  you may not use this file except in compliance with the License.         }
{  You may obtain a copy of the License at                                  }
{                                                                           }
{      http://www.apache.org/licenses/LICENSE-2.0                           }
{                                                                           }
{  Unless required by applicable law or agreed to in writing, software      }
{  distributed under the License is distributed on an "AS IS" BASIS,        }
{  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{  See the License for the specific language governing permissions and      }
{  limitations under the License.                                           }
{                                                                           }
{***************************************************************************}

unit Spring.Serialization.Options;

{$I Spring.inc}

interface

uses
  Classes,
  SysUtils,
  Rtti,
  TypInfo,
  Generics.Collections,
  Spring,
  Spring.Collections;

type
  TSerializableMemberKind = (mkProperty, mkField);

  TSerializableMembersSet = set of TSerializableMemberKind;

  TMemberVisibilitySet = set of TMemberVisibility;

  TMembersOptionsBase = class
  private
    fSerialize: Boolean;
    procedure SetSerialize(const Value: boolean);
  published
    property Serialize: boolean read fSerialize write SetSerialize;
  end;

  TClassesOptions = class(TMembersOptionsBase)
  private
    fVisibilities: TMemberVisibilitySet;
    fMembers: TSerializableMembersSet;
    procedure SetMembers(const Value: TSerializableMembersSet);
    procedure SetVisibilities(const Value: TMemberVisibilitySet);
  published
    property Members: TSerializableMembersSet read fMembers write SetMembers;
    property Visibilities: TMemberVisibilitySet read fVisibilities write SetVisibilities;
  end;

  TInterfacesOptions = class(TMembersOptionsBase)
  end;

  TRecordsOptions = class(TMembersOptionsBase)
  private
    fVisibilities: TMemberVisibilitySet;
    procedure SetVisibilities(const Value: TMemberVisibilitySet);
  published
    property Visibilities: TMemberVisibilitySet read fVisibilities write SetVisibilities;
  end;

  TCommonOptions = class
  private
    fActivateNilProperties: boolean;
    fSupportCycles: boolean;
    fSerializeReadOnlyScalarProperties: boolean;
    procedure SetActivateNilProperties(const Value: boolean);
    procedure SetSerializeReadOnlyScalarProperties(const Value: boolean);
    procedure SetSupportCycles(const Value: boolean);
  published
    property ActivateNilProperties: boolean read fActivateNilProperties write SetActivateNilProperties;
    property SupportCycles: boolean read fSupportCycles write SetSupportCycles;
    property SerializeReadOnlyScalarProperties: boolean read fSerializeReadOnlyScalarProperties write SetSerializeReadOnlyScalarProperties;
  end;

  TSerializationOptions = class
  private
    fClasses: TClassesOptions;
    fCommon: TCommonOptions;
    fInterfaces: TInterfacesOptions;
    fRecords: TRecordsOptions;
    function GetClasses: TClassesOptions;
    function GetCommon: TCommonOptions;
    function GetInterfaces: TInterfacesOptions;
    function GetRecords: TRecordsOptions;
  published
    property Classes: TClassesOptions read GetClasses;
    property Interfaces: TInterfacesOptions read GetInterfaces;
    property Records: TRecordsOptions read GetRecords;
    property Common: TCommonOptions read GetCommon;
  end;

implementation

{ TSerializationOptions }

function TSerializationOptions.GetClasses: TClassesOptions;
begin
  if not Assigned(fClasses) then
    fClasses := TClassesOptions.Create;

  Result := fClasses;
end;

function TSerializationOptions.GetCommon: TCommonOptions;
begin
  if not Assigned(fCommon) then
    fCommon := TCommonOptions.Create;

  Result := fCommon;
end;

function TSerializationOptions.GetInterfaces: TInterfacesOptions;
begin
  if not Assigned(fInterfaces) then
    fInterfaces := TInterfacesOptions.Create;

  Result := fInterfaces;
end;

function TSerializationOptions.GetRecords: TRecordsOptions;
begin
  if not Assigned(fRecords) then
    fRecords := TRecordsOptions.Create;

  Result := fRecords;
end;

{ TClassesOptions }

procedure TClassesOptions.SetMembers(const Value: TSerializableMembersSet);
begin
  fMembers := Value;
end;

procedure TClassesOptions.SetVisibilities(const Value: TMemberVisibilitySet);
begin
  fVisibilities := Value;
end;

{ TMembersOptionsBase }

procedure TMembersOptionsBase.SetSerialize(const Value: boolean);
begin
  fSerialize := Value;
end;

{ TRecordsOptions }

procedure TRecordsOptions.SetVisibilities(const Value: TMemberVisibilitySet);
begin
  fVisibilities := Value;
end;

{ TCommonOptions }

procedure TCommonOptions.SetActivateNilProperties(const Value: boolean);
begin
  fActivateNilProperties := Value;
end;

procedure TCommonOptions.SetSerializeReadOnlyScalarProperties(
  const Value: boolean);
begin

end;

procedure TCommonOptions.SetSupportCycles(const Value: boolean);
begin
  fSupportCycles := Value;
end;

end.
